/**
 * Copyright (C) 2006 OQube 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *  
 * author: Arnaud Bailly - abailly AT oqube DOT com
 * created: 14 juin 2006
 */
package speculoos.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


/**
 * A class that holds a chain of mappers.
 *  This class collects
 * a sequence of mappers at configuration time, then executes the sequence at
 * runtime over thu input object. It is a generalization of the validators
 * mappers that allows arbitrary modification of input object.
 * 
 * @author nono
 * 
 */
public class ChainMapper implements MapperCollector, Mapper {

	private List mappers = new ArrayList();

	private String name;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see speculoos.core.MapperCollector#addMapper(speculoos.core.Mapper)
	 */
	public void addMapper(Mapper m) {
		mappers.add(m);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see speculoos.core.MapperCollector#addMappers(java.util.Collection)
	 */
	public void addMappers(Collection col) {
		mappers.addAll(col);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see speculoos.core.MapperCollector#getMappers()
	 */
	public Collection getMappers() {
		return Collections.unmodifiableList(mappers);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see speculoos.core.Mapper#map(java.lang.Object, java.util.Map)
	 */
	public Object map(Object input, Map param) throws MapperException {
		for(Iterator it = mappers.iterator();it.hasNext();) {
			input = ((Mapper)it.next()).map(input,param);
		}
		return input;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see speculoos.core.Mapper#getName()
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

}
